package Controller_Servlet;
import Model_Db.DbUtil;
import java.util.*;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.text.DecimalFormat;


@WebServlet("/paramservlet")
public class paramservlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
		DbUtil a=new DbUtil();
	    request.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=UTF-8");
	    String msg=request.getParameter("sel");
		String sql_symptoms="select * from t_casehistory where ch_clinicaldiagnosis='"+msg+"'";
		ResultSet rs_symptom=a.QuerySQL(sql_symptoms);
		rs_symptom.last();
		rs_symptom.beforeFirst();
		List<String> list=new ArrayList<String>();
		while(rs_symptom.next()){
			String symptom=rs_symptom.getString("ch_Degree_symptoms");
			list.add(symptom);
		}
		rs_symptom.last();
		float all=rs_symptom.getRow();
		float cured=0;
		float milded=0;
		float moderated=0;
		float severed=0;
		float deaded=0;
		float normaled=0;
		for(int i=0;i<rs_symptom.getRow();i++){
		if(list.get(i).equals("已治愈")==true){
			cured=cured+1;
			continue;
		}
		if(list.get(i).equals("轻度")==true){
			milded=milded+1;
			continue;
		}
		if(list.get(i).equals("中度")==true){
			moderated=moderated+1;
			continue;
		}
		if(list.get(i).equals("重度")==true){
			severed=severed+1;
			continue;
		}
		if(list.get(i).equals("死亡")==true){
			deaded=deaded+1;
			continue;
		}
		if(list.get(i).equals("正常")==true){
			normaled=normaled+1;
		}
		}
		float cure=(cured/(all-normaled))*100;
		float mild=(milded/(all-normaled))*100;
		float moderate=(moderated/(all-normaled))*100;
		float severe=(severed/(all-normaled))*100;
		float dead=(deaded/(all-normaled))*100;
		float normal=(normaled/all)*100;
		DecimalFormat decimalFormat = new DecimalFormat("0.00");
		String str_cure=decimalFormat.format(cure)+"%";
		String str_mild=decimalFormat.format(mild)+"%";
		String str_moderate=decimalFormat.format(moderate)+"%";
		String str_severe=decimalFormat.format(severe)+"%";
		String str_dead=decimalFormat.format(dead)+"%";
		String str_normal=decimalFormat.format(normal)+"%";
	    
	    
	    
		String json="";
		ObjectMapper mapper = new ObjectMapper();
		json ="{\"msg\":\"" + msg + "\",\"str_cure\":\""+ str_cure +"\",\"str_mild\":\""+ str_mild +"\",\"str_moderate\":\""+str_moderate+"\",\"str_severe\":\""+str_severe+"\",\"str_dead\":\""+str_dead+"\",\"str_normal\":\""+str_normal+"\"}";
		mapper.writeValue(response.getWriter(), json);
	    
	    

	}catch(SQLException e) {
    }
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
